/**
 * &copy; 2006 NewInstance.it
 */
package it.newinstance.jrainbow.converter;

import it.newinstance.jrainbow.source.Tag;

import java.io.IOException;
import java.io.Writer;

/**
 * @author Luigi R. Viggiano
 * @version $Id: HTMLConverter.java 138 2007-06-12 12:23:29Z luigi.viggiano $
 */
public abstract class HTMLConverter implements Converter {
    
    private Writer writer;
    
    public void setWriter(Writer writer) {
        this.writer = writer;
    }
    
    protected Writer getWriter() {
        return writer;
    }
    
    protected void emitSpanStart(Tag element) throws IOException {
        emitSpanStart(String.valueOf(element).toLowerCase());
    }

    protected void emitSpanStart(String value) throws IOException {
        getWriter().write("<span class=\"" + value + "\">");
    }

    protected void emitSpanEnd() throws IOException {
        getWriter().write("</span>");
    }
    
    protected static String htmlEncode(String string) {
        return string
            .replaceAll("&", "&amp;")
            .replaceAll("<", "&lt;")
            .replaceAll(">", "&gt;")
            .replaceAll("\"", "&quot;")
            ;
    }    
}
